package com.example.snackfood.services.impl;

import com.example.snackfood.dao.SnackFoodUsersDao;
import com.example.snackfood.entity.Users;
import com.example.snackfood.services.IsnackFoodUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: snackFood
 * @description:
 * @author: lydms
 * @create: 2023-11-22 21:17
 **/


@Service
public class SnackFoodUsersService implements IsnackFoodUsersService {

    @Autowired
    private SnackFoodUsersDao snackFoodDao;
    /**
     * 增加用户
     * @param user
     */
    public void saveUser(Users user) {
        snackFoodDao.save(user);
    }

    /**
     * 查所有用户信息
     * @return
     */
    public List<Users> findAll() {
        return snackFoodDao.findAll();
    }

    /**
     * 根据id查
     * @return
     */
    public Users get(Long id) {
        Criteria criteriaid=Criteria.where("id").is(id);
        Criteria orCriteria = new Criteria();
        orCriteria.orOperator(criteriaid);
        Query query=new Query(orCriteria);
        return  snackFoodDao.get(query);

    }

    /**
     * 改用户信息
     * @param users
     */
    public void update(Users users) {
        Query query=new Query(Criteria.where("id").is(users.getId()));
        Update update=new Update();
        update.set("username",users.getUsername());
        update.set("password",users.getPassword());
        update.set("email",users.getEmail());
        update.set("phone",users.getPhone());
        update.set("address",users.getAddress());
        snackFoodDao.update(query,update);
    }

    @Override
    public void delete(Long ids) {
        Query query=new Query(Criteria.where("id").is(ids));
        snackFoodDao.delete(query);
    }


    @Override
    public Users selectUserByUserName(String username) {
        Query query=new Query(Criteria.where("username").is(username));
        return snackFoodDao.selectUserByUserName(query);
    }
}
